<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/10/22
 * Time: 9:17
 */

namespace app\common\entity;


use think\Model;
use think\Request;

class UserPackage extends Model
{

    protected $table = 'user_package';


    public static function getTypes()
    {
        return [
            1 => '一次性',
            2 => '每天',
        ];
    }

    public static function getOneTypes($types)
    {
        switch ($types) {
            case 1:
                return '一次性';
            case 2:
                return '每天';
            default:
                return '';
        }
    }

    /*
     * @param 商品ID
     * 根据商品ID 查询商品图
     * */
    public function getGoodsPic($id){
        $goods = new self();
        $goodsList = $goods->where(array('id'=>$id))->find();
        $goodsList['goods_content'] = htmlspecialchars_decode($goodsList['level_content']);
        //提取商品内容中的图片
        $pattern='/<img((?!src).)*src[\s]*=[\s]*[\'"](?<src>[^\'"]*)[\'"]/i';
        preg_match_all($pattern,$goodsList['goods_content'],$out);
        return $out[2];
    }



    public static function getAllCate()
    {
        $entity = new self();
        $list = $entity->select();
        foreach ($list as $v){
            if ($v['level'] == '1'){$v['level'] = '微型矿机';}
            if ($v['level'] == '2'){$v['level'] = '小型矿机';}
            if ($v['level'] == '3'){$v['level'] = '中型矿机';}
            if ($v['level'] == '4'){$v['level'] = '大型矿机';}
            if ($v['level'] == '5'){$v['level'] = '超级矿机';}

        }
        return $list;
    }
    public static function getOneCate($level)
    {

        switch ($level) {
            case 1:
                return '微型矿机';
            case 2:
                return '小型矿机';
            case 3:
                return '中型矿机';
            case 4:
                return '大型矿机';
            case 5:
                return '超级矿机';
            default:
                return '';
        }

        return $level;
    }

    #更新
    public function updpack(Request $request){
        $id = $request->get('id');
        $level = $request->post('level');
        $money = $request->post('money');
        $times = $request->post('times');
        $day_power = $request->post('day_power');
        $recast = $request->post('recast');
        $total = $request->post('total');
        $share = $request->post('share');

        $data = [
            'level' => $level,
            'money' => $money,
            'times' => $times,
            'day_power' => $day_power,
            'recast' => $recast,
            'total' => $total,
            'share' => $share,
            'update_time' => time()
        ];
        $res = $this->where('id',$id)->update($data);
        return $res;
    }

    #新增
    public function inspackage(Request $request){
        $id = $request->get('id');
        $level = $request->post('level');
        $money = $request->post('money');
        $times = $request->post('times');
        $day_power = $request->post('day_power');
        $recast = $request->post('recast');
        $total = $request->post('total');
        $share = $request->post('share');

        $data = [
            'level' => $level,
            'money' => $money,
            'times' => $times,
            'day_power' => $day_power,
            'recast' => $recast,
            'total' => $total,
            'share' => $share,
            'create_time' => time()
        ];
        $res = $this->insert($data);
        return $res;
    }

}